package com.mantru.iotpot.setting

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.View
import android.widget.Toast
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import com.mantru.iotpot.R
import com.mantru.iotpot.Util.ToastUtils
import com.mantru.iotpot.databinding.ActivityUpgradeAppBinding

class UpgradeAppActivity : AppCompatActivity() {
    private val viewModel by viewModels<UpgradeAppViewModel>()
    private lateinit var binding: ActivityUpgradeAppBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = DataBindingUtil.setContentView(this, R.layout.activity_upgrade_app)
        binding.viewModel = viewModel // 绑定viewModel
        binding.lifecycleOwner = this // 允许 LiveData 自动更新 UI

        //设置控件交互事件
        setContentAction()
        //观察viewModel返回结果
        observeViewModelResult()
        //获取文件大小和时间
        viewModel.getUpgradeValue()
        viewModel.needUpgradeTip = false
    }

    //设置控件交互事件
    private fun setContentAction(){

        //返回按钮
        binding.appUpgradeBack.setOnClickListener{
            finish()
        }

        //升级按钮
        binding.appUpgradeDownload.setOnClickListener {
            //获取升级版本数据
            viewModel.getUpgradeValue()
            viewModel.needUpgradeTip = true
        }

        //取消升级按钮
        binding.appUpgradeStopDownload.setOnClickListener {
            // 取消下载
            viewModel.cancelDownload()

            binding.appUpgradeProgress.visibility = View.GONE
            binding.appUpgradeStopDownload.visibility = View.GONE
            binding.appUpgradeDownload.visibility = View.VISIBLE
            binding.appUpgradeTip.text = "提示：下载和安装过程中将停止全部炒菜控制"

            //Toast.makeText(this, "已取消下载", Toast.LENGTH_SHORT).show()
            ToastUtils.show(this, "已取消下载")

        }
    }

    //观察viewModel返回结果
    @SuppressLint("SetTextI18n")
    private fun observeViewModelResult(){
        //获取升级版本数据返回
        viewModel.downloadResult.observe(this) { exists ->
            exists?.let {
                if (it) {
                    binding.appUpgradeProgress.visibility = View.VISIBLE
                    binding.appUpgradeStopDownload.visibility = View.VISIBLE
                    binding.appUpgradeDownload.visibility = View.GONE
                    binding.appUpgradeTip.text = "提示：下载过程中请不要断电"
                } else {
                    //Toast.makeText(this, "网络请求失败，请重试", Toast.LENGTH_SHORT).show()
                    ToastUtils.show(this, "网络请求失败，请重试")
                }
            } ?: run {
                // 处理网络请求异常的情况
                //Toast.makeText(this, "网络请求失败，请重试", Toast.LENGTH_SHORT).show()
                ToastUtils.show(this, "网络请求失败，请重试")
            }
        }

        //进度条显示并返回
        viewModel.downloadProgress.observe(this) { number ->
            number?.let {
                binding.appUpgradeProgressProgressbar.progress = number
                binding.appUpgradeProgressText.text = "$number%"
            }
        }

        //文件数据
        viewModel.fileSize.observe(this) { size ->
            size?.let {
                binding.appUpgradeSizeText.text = size.toString() + "字节"
            }
        }
        viewModel.lastModified.observe(this) { time ->
            time?.let {
                binding.appUpgradeTimeText.text = time
            }
        }
    }
}